﻿using System.Collections.Generic;
using RecipeModule;
using UnityEngine;
using UnityEngine.UI;

namespace View
{
    public class RecipeDishUnit
    {
        private GameObject _gameObject;
        private RectTransform _transform;
        private RecipeDishGrid[] _grids;

        public RecipeDishUnit(GameObject gameObject)
        {
            _gameObject = gameObject;
            _transform = (RectTransform) _gameObject.transform;
            GameObject gridPrefab = _transform.Find("Grid").gameObject;

            _grids = new RecipeDishGrid[8];
            for (var i = 0; i < 8; i++)
            {
                GameObject goDish = GameObject.Instantiate(gridPrefab);
                RecipeDishGrid grid = new RecipeDishGrid(goDish);
                grid.AddTo(gridPrefab.transform.parent);
                _grids[i] = grid;
            }

            gridPrefab.SetActive(false);
        }

        public void Dispose()
        {
            for (var i = 0; i < _grids.Length; i++)
            {
                _grids[i].Dispose();
            }

            _grids = null;
        }

        public void Update(List<RecipeDish> listDish)
        {
            if (listDish != null)
            {
                for (var i = 0; i < listDish.Count; i++)
                {
                    _grids[i].SetActive(true);
                    _grids[i].Update(listDish[i]);
                }

                for (var i = listDish.Count; i < 8; ++i)
                {
                    _grids[i].SetActive(false);
                }
            }
            else
            {
                for (var i = 0; i < _grids.Length; i++)
                {
                    _grids[i].SetActive(false);
                }
            }
        }

        private class RecipeDishGrid
        {
            private GameObject _gameObject;
            private RectTransform _transform;
            private SquareDishIconProxy _iconProxy;
            private Text _textNum;

            public RecipeDishGrid(GameObject gameObject)
            {
                _gameObject = gameObject;
                _transform = (RectTransform) _gameObject.transform;

                _iconProxy = new SquareDishIconProxy();
                _iconProxy.AddTo(_transform.Find("Icon"));
                _textNum = _transform.Find("Text_Num").GetComponent<Text>();
            }

            public void Dispose()
            {
                GameObject.Destroy(_gameObject);
            }

            public void SetActive(bool isActive)
            {
                _gameObject.SetActive(isActive);
            }

            public void Update(RecipeDish dish)
            {
                _iconProxy.SetIconId(dish.dishId);
                _textNum.text = "x" + dish.count;  
            }

            public void AddTo(Transform parent)
            {
                _transform.SetParent(parent, false);
            }

            public RectTransform GetTransform()
            {
                return _transform;
            }
        }
    }
}